package forward.Module.Modules.Combat;



import forward.Module.Module;
import forward.Module.ModuleType;
import forward.Util.TimeHelper;
import forward.Util.TimerUtil;
import forward.api.EventHandler;
import forward.api.Events.World.EventPreUpdate;
import forward.api.Events.World.EventTick;
import forward.api.Value.Numbers;
import forward.api.Value.Option;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.item.ItemSword;
import net.minecraft.network.play.client.C08PacketPlayerBlockPlacement;
import org.apache.commons.lang3.RandomUtils;

import java.util.Random;


public class AutoClicker extends Module {
    private final TimerUtil timer = new TimerUtil();
    private Numbers<Double> cps = new Numbers<Double>("CPSMax", "CpsMax",9.0, 1.0, 20.0,1.0);
    private Numbers<Double> cpsMin = new Numbers<Double>("CPSMin", "CpsMin",9.0, 1.0, 20.0,1.0);
   // private Option<Boolean> autoblock = new Option<Boolean>("AutoBlock","AutoBlock", false);
    public boolean doBlock = true;
    public AutoClicker() {
        super("AutoClicker",new String[]{"Autoclicker"}, ModuleType.Combat);
        //super("AutoClicker", Keyboard.KEY_NONE, ModuleType.Combat,"auto Attack when you hold the attack button");
        this.addValues(this.cps,cpsMin);
    }

    @EventHandler
    public void onTick(EventTick event) {
        try {
            this.setSuffix(this.cps.getValue().toString());
            int key = mc.gameSettings.keyBindAttack.getKeyCode();
            if (mc.gameSettings.keyBindAttack.isKeyDown()) {
                float delays = (float) (getRandomDoubleInRange(cpsMin.getValue(), cps.getValue()) + 2);
                if (timer.delay(delays * 10)) {
                    mc.thePlayer.swingItem();
                    KeyBinding.onTick(key);
                    try {
                        if (mc.objectMouseOver.entityHit != null) {
                            mc.playerController.attackEntity(mc.thePlayer, Minecraft.getMinecraft().objectMouseOver.entityHit);


                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    timer.reset();
                }
            }
        } catch (NullPointerException e){
//            Helper.sendMessage("傻逼，你他妈会不会写代码，你写的代码全是" + e.toString());
        }
    }
    public static double getRandomDoubleInRange(double minDouble, double maxDouble) {
        return minDouble >= maxDouble ? minDouble : new Random().nextDouble() * (maxDouble - minDouble) + minDouble;
    }
}

